package com.lanou3g.code0417.dirty;

/**
 * If there are no bugs, it was created by Chen FengYao on 2018/4/17;
 * Otherwise, I don't know who created it either
 */

public class Utils {
    private final Object lock = new Object();
    // a 1
    // Thread1 : b 2
    // Thread2 : c 3
    public void updatePerson(Person person
            , String name, int age) {
        synchronized (lock) {
            person.setName(name);
            // 让线程沉睡1s,为了让脏读效果更容易出现
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            person.setAge(age);
        }
    }


    public void showPerson(Person person){

        synchronized (lock) {
            System.out.println("姓名:" + person.getName());
            System.out.println("年龄:" + person.getAge());
        }
    }
}
